import request from '@/utils/request';
import { download, toFormData, checkDownloadRes } from '@/utils/common';

/**
 * 分页查询用户和角色关联
 */
export async function pageMesUserRoles(params) {
  const res = await request.get('/mes/mesUserRole/list', { params });
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 查询全部用户和角色关联
 */
export async function listMesUserRoles(params) {
  const res = await request.get('/mes/mesUserRole/list', { params });
  if (res.data.code === 200) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 根据id查询用户和角色关联
 */
export async function getMesUserRole(id) {
  const res = await request.get('/mes/mesUserRole/' + id);
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 添加用户和角色关联
 */
export async function addMesUserRole(data) {
  const res = await request.post('/mes/mesUserRole', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改用户和角色关联
 */
export async function updateMesUserRole(data) {
  const res = await request.put('/mes/mesUserRole', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 删除用户和角色关联
 */
export async function removeMesUserRole(id) {
  const res = await request.delete('/mes/mesUserRole/' + id);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 批量删除用户和角色关联
 */
export async function removeMesUserRoles(ids) {
  const res = await request.delete('/mes/mesUserRole/' + ids.join());
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 导出用户和角色关联
 */
export async function exportMesUserRoles(params) {
  const res = await request({
    url: '/mes/mesUserRole/export',
    method: 'POST',
    data: toFormData(params),
    responseType: 'blob'
  });
  await checkDownloadRes(res);
  download(res.data, `mesUserRole_${Date.now()}.xlsx`);
}
